package com.test.nio;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileHole {
	public static void main(String[] args) throws Exception {
		File temp = File.createTempFile("E:/holy.txt", null);
		RandomAccessFile raf = new RandomAccessFile(temp, "rw");
		FileChannel channel = raf.getChannel();
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(100);
		putData(0, byteBuffer, channel);
		putData(5000000, byteBuffer, channel);
		putData(50000, byteBuffer, channel);
		System.out.println("Wrote temp file '" + temp.getPath() + "', size=" + channel.size());
		channel.close();
		raf.close();
	}
	public static void putData(int position, ByteBuffer buffer, FileChannel channel) throws Exception {
		String string = "*<-- location " + position;
		buffer.clear();
		buffer.put(string.getBytes("US-ASCII"));
		buffer.flip();
		channel.position(position);
		channel.write(buffer);
	}
}
